package com.xinfu.mooddiaryjava.ui.page;


import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.kunminx.architecture.ui.adapter.SimpleBaseBindingAdapter;
import com.xinfu.mooddiaryjava.R;
import com.xinfu.mooddiaryjava.bridge.state.ContentListViewModel;
import com.xinfu.mooddiaryjava.data.bean.StateBean;
import com.xinfu.mooddiaryjava.databinding.FragmentContentListBinding;
import com.xinfu.mooddiaryjava.databinding.RecyclerFragmentContentListBinding;
import com.xinfu.mooddiaryjava.ui.base.BaseFragment;
import com.xinfu.mooddiaryjava.widget.AlertDialog;

import java.util.ArrayList;
import java.util.List;

public class ContentListFragment extends BaseFragment {

    private ContentListViewModel mViewModel;
    private FragmentContentListBinding mBinding;
    private SimpleBaseBindingAdapter<StateBean, RecyclerFragmentContentListBinding> mAdapter;
    private Fragment fragment;
    private List<StateBean> list = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = getFragmentViewModelProvider(this).get(ContentListViewModel.class);
        fragment = this;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content_list, container, false);
        mBinding = FragmentContentListBinding.bind(view);
        mBinding.setVm(mViewModel);
        mBinding.setClick(new ClickProxy());
        return view;
    }

    private long firstClickTime;
    private long secondClickTime;
    private long stillTime;
    private boolean isUp = false;
    private boolean isDoubleClick = false;


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        StateBean stateBean = new StateBean(getArguments().getString("state"));
        mBinding.setTitle(stateBean);
        // TODO: Use the ViewModel
        mAdapter = new SimpleBaseBindingAdapter<StateBean, RecyclerFragmentContentListBinding>(getContext(), R.layout.recycler_fragment_content_list) {
            @SuppressLint("ClickableViewAccessibility")
            @Override
            protected void onSimpleBindItem(RecyclerFragmentContentListBinding binding, StateBean item, RecyclerView.ViewHolder holder) {
                binding.fragmentContentListRecyclerTv.setText(item.getText());
                binding.fragmentContentListRecyclerIv.setVisibility(item.getTextOK() ? View.VISIBLE : View.GONE);
                binding.fragmentContentListRecycler.setBackgroundColor(holder.getLayoutPosition()%2 == 0 ?getActivity().getResources().getColor(R.color.colorBlue) : getActivity().getResources().getColor(R.color.colorYellow));
                binding.fragmentContentListRecycler.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()){
                            case MotionEvent.ACTION_DOWN:
                                isUp = false;
                                if(firstClickTime == 0 & secondClickTime == 0){//第一次点击
                                    firstClickTime = System.currentTimeMillis();
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            if(!isUp){//长按
                                                AlertDialog myDialog = new AlertDialog(getActivity()).builder();
                                                myDialog.setGone().setTitle("").setMsg("确定删除此日记?").setNegativeButton("取消", R.color.gray_a5, new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View view) {
                                                        myDialog.dismiss();
                                                    }
                                                }).setPositiveButton("确定", R.color.theme_color, new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        list.remove(holder.getLayoutPosition());
                                                        notifyDataSetChanged();
                                                        myDialog.dismiss();
                                                    }
                                                }).show();
                                                firstClickTime = 0;
                                                secondClickTime = 0;
                                                isDoubleClick = false;
                                            }else {//单击
                                                if(!isDoubleClick){
                                                    Bundle bundle = new Bundle();
                                                    bundle.putString("title", item.getText());
                                                    NavHostFragment
                                                            .findNavController(fragment)
                                                            .navigate(R.id.action_contentListFragment_to_contentFragment, bundle);
                                                }
                                                isDoubleClick = false;
                                                firstClickTime = 0;
                                                secondClickTime = 0;
                                            }
                                        }
                                    }, 500);

                                }else {
                                    secondClickTime = System.currentTimeMillis();
                                    stillTime = secondClickTime-firstClickTime;
                                    if ( stillTime < 500) {//双击
                                        Bundle bundle = new Bundle();
                                        bundle.putString("title", item.getText());
                                        NavHostFragment
                                                .findNavController(fragment)
                                                .navigate(R.id.action_contentListFragment_to_contentFragment, bundle);
                                        isDoubleClick = true;
                                        firstClickTime = 0;
                                        secondClickTime = 0;
                                    }
                                }
                                break;
                            case MotionEvent.ACTION_UP:
                                isUp = true;
                                break;
                        }
                        return true;
                    }
                });
            }
        };
        mBinding.fragmentContentListRv.setAdapter(mAdapter);
        list.clear();
        list.add(new StateBean("标题1", true));
        list.add(new StateBean("标题2", false));
        list.add(new StateBean("标题3", true));
        list.add(new StateBean("标题4", true));
        list.add(new StateBean("标题5", false));
        list.add(new StateBean("标题6", true));

        mAdapter.setList(list);
        mAdapter.notifyDataSetChanged();


    }

    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

    }
}
